package com.efesco.system;

import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.common.*;
import com.efesco.common.Tools;

/**
 * employeeRole_employeeDatasetListener
 */
public class employeeRole_employeeDatasetListener extends AbstractDatasetListener {
  public boolean beforeLoadData(Dataset dataset)
     throws Exception {
    String sql="",foreign_enterprise_id="";
  	
  	DoradoContext context = DoradoContext.getContext();
    Object obj = context.getAttribute(context.SESSION, "User");
    if (obj != null) {
    	User user = (User) obj;
   		foreign_enterprise_id=user.getForeignEnterpriseId();
   		if (!Tools.toStr(dataset.parameters().getString("role_ins_id")).equals("")
   	    		&& !Tools.toStr(dataset.parameters().getString("role_ins_id")).equals("null")	)  {
   	    	sql = "select emp.employee_id,emp.employee_code,emp.employee_name " +
   	    			"from s_fe_employee emp ,s_fe_emp_role r " +
   	    			"where emp.employee_id=r.employee_id and emp.foreign_enterprise_id="+foreign_enterprise_id+
					" and emp.is_valid=1 and r.role_ins_id="+Tools.toStr(dataset.parameters().getString("role_ins_id"));
   	    }
   		else  {
   			sql = "select emp.employee_id,emp.employee_code,emp.employee_name " +
   			"from s_fe_employee emp  " +
   			"where emp.is_valid=1 and emp.foreign_enterprise_id="+foreign_enterprise_id;
   		}
   		
   		if (!Tools.toStr(dataset.parameters().getString("employee_code")).equals("")
   	    		&& !Tools.toStr(dataset.parameters().getString("employee_code")).equals("null")	)  {
   			sql = sql+" and emp.employee_code like '%"+Tools.toStr(dataset.parameters().getString("employee_code"))+"%'";
   		}
   		if (!Tools.toStr(dataset.parameters().getString("employee_name")).equals("")
   	    		&& !Tools.toStr(dataset.parameters().getString("employee_name")).equals("null")	)  {
   			sql = sql+" and emp.employee_name like '%"+Tools.toStr(dataset.parameters().getString("employee_name"))+"%'";
   		}
    	SqlDataset sqlDataset=(SqlDataset)dataset;
    	sqlDataset.setSql(sql);
    }
    return true;
  }

}
